﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Alerts_Administration_2010.classes.views.templates
{
    public class AlertFrequencyTemplate : AlertViewTemplateBase
    {
        private RadioButtonList _rbl;
        private Panel _panel;
        private DropDownList _days;
        private DropDownList _hours;

        protected override System.Web.UI.Control CreateControl()
        {
            _panel = new Panel();
            _days = new DropDownList() { ID = "{DE72A2C6-21FB-464E-9171-834A3A28DA6E}" };
            _hours = new DropDownList() { ID = "{8242E949-4695-40C8-BAC5-614FEFFF9719}" };
            _rbl = new RadioButtonList();
            _rbl.AutoPostBack = true;
            _rbl.ID = "{435B9F54-1AAA-4BA6-BC35-ACCDC0C15E93}";
            _rbl.PreRender += new EventHandler(_rbl_PreRender);
            
            _panel.Controls.Add(_rbl);
            _panel.Controls.Add(_hours);
            _panel.Controls.Add(_days);

            return _panel;
        }

        private void _rbl_PreRender(object sender, EventArgs e)
        {
            _days.Enabled = _rbl.SelectedValue != "Immediate";
            _hours.Enabled = _rbl.SelectedValue == "Daily";
        }

        protected override string GetBoundPropertyNames()
        {
            return "AlertFrequency,AlertTime";
        }

        protected override object GetValue(string p)
        {
            if (p == "AlertFrequency") return _rbl.SelectedValue;
            switch (_rbl.SelectedValue)
            {
                case "Daily":
                    {
                        int hour = int.Parse(_hours.SelectedValue);
                        DateTime potentialAlertTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0);
                        if (DateTime.Now >= potentialAlertTime)
                        {
                            potentialAlertTime = potentialAlertTime.AddDays(1);
                        }
                        return potentialAlertTime;
                    }
                case "Immediate":
                    return null;
                case "Weekly":
                    {
                        int hour = int.Parse(_hours.SelectedValue);
                        DateTime potentialAlertTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, hour, 0, 0);
                        while ((DateTime.Now > potentialAlertTime) || (potentialAlertTime.DayOfWeek.ToString() != _days.SelectedValue))
                        {
                            potentialAlertTime = potentialAlertTime.AddDays(1);
                        }
                        return potentialAlertTime;
                    }
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        protected override void BindControl(DetailsViewItemBLL boundItem)
        {
            foreach (string alertFrequency in boundItem.AlertFrequencies.Keys)
            {
                var li = new ListItem(boundItem.AlertFrequencies[alertFrequency]);
                li.Value = alertFrequency;
                if (li.Value == boundItem.AlertFrequency) li.Selected = true;
                _rbl.Items.Add(li);
            }

            for (DateTime dt = new DateTime(2010, 9, 19); dt < new DateTime(2010, 9, 26); dt = dt.AddDays(1))
            {
                var li = new ListItem(dt.DayOfWeek.ToString(), dt.DayOfWeek.ToString());
                if (li.Value == boundItem.AlertTime.DayOfWeek.ToString()) li.Selected = true;
                _days.Items.Add(li);
            }

            for (int i = 0; i < 24; i++)
            {
                var li = new ListItem(((i + 11) % 12 + 1).ToString() + ":00 " + ((i / 12) == 0 ? "AM" : "PM"), i.ToString());
                if (li.Value == boundItem.AlertTime.Hour.ToString()) li.Selected = true;
                _hours.Items.Add(li);
            }
        }
    }
}
